<?php
namespace View;
//Vy för inläggen
class PostView {

	private $postID = 'postid';
	private $title = 'title';
	private $content = 'content';
	private $submit = 'submit';
	private $edit = 'edit';
	private $editsubmit = 'editsubmit';
	private $delete = 'delete';
	private $editbutton = 'editbutton';

	const POST_EMPTY = 0;
	const POST_SUCCESS = 1;
	const POST_FAILED = 2;
	const POST_SAVED = 3;
	const POST_DELETED = 4;
	const POST_NOT_DELETED = 5;
	//Visar en post när man tryckt på vald post
	public function showpost(\Model\post $post, \Model\Database $database) {

		$listLink = NavigationView::getpostListLink();
		$this -> title = $post -> getTitle();
		$this -> content = $post -> getContent();
		$date = $post -> getDate();
		$this -> postID = $post -> getId();

		$loginhandler = new \Model\LoginHandler($database);
		$posthandler = new \Model\PostHandler($database);
		$deletebutton = "";

		//Om man är inloggad så visas deletebutton
		if ($loginhandler -> IsLoggedIn() == true) {
			$deletebutton = $this -> MakeChangesButtons();
		}

		return "
			
				<div class='post'>
					<h2 class='title'>$this->title</h2>
					<div class='entry'>
					<p>$this->content</p>
					</div>
					<div class='byline'>
					<p class='meta'>$date</p>
					</div>
					$deletebutton
					</br>
					<a href='$listLink'>Tillbaka till listan</a>
				</div>
				
				<div id='comments'></div>";
	}

	//Visar lista med poster, när $ListorRoll är true visas hela poster med allt, och när de e false så visas bara länkar med titel
	public function ShowListOfPosts(\Model\postArray $postArray, $ListorRoll) {
		if ($ListorRoll == true) {
			$ret = "";

			foreach ($postArray->get() as $post) {
				$linkTopost = NavigationView::getpostLink($post);
				$ret .= "<div class='post'>";
				$ret .= "<h2 class='title'><a href='$linkTopost'>" . $post -> getTitle() . "</a></h2>";
				$ret .= "<div class='entry'> <p>" . $post -> getContent() . "</p></div>";
				$ret .= "<div class='byline'>";
				$ret .= "<p class='meta'>" . $post -> getDate() . "</p>";
				$ret .= "<p class='links'><a href='$linkTopost'>Läs mer</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href='$linkTopost#comments' title='b0x w'>Kommentarer</a></p>";
				$ret .= "</div>";
				$ret .= "</div>";
			}

			return $ret;
		} else {

			$ret = "";
			$i = 0;
			foreach ($postArray->get() as $post) {
				$linkTopost = NavigationView::getpostLink($post);
				$ret .= "<li><a href='$linkTopost'>" . $post -> getTitle() . "</a></li>";
				if (++$i > 4)
					break;
			}

			return $ret;
		}

	}
	//Visar formulär för nytt inlägg.
	public function NewPostForm() {

		$xhtml = "
		<h3>Nytt inlägg:</h3>
			<form action='index.php?page=newpost' method='post' class='post'> 
				<p>Titel:</p><input type='text' name='$this->title' style='width:350px; height:30px; '/></br>
				<p>Inlägg:</p><textarea class='ckeditor' type='text' name='$this->content' style='width:550px; height:320px;' ></textarea></br>
				<input type='submit' name='$this->submit' value='Skapa' class='delete' style='margin-top:10px;'/>
				</form>";
		return $xhtml;
	}
	//Edit formulär
	public function EditPostForm($post) {
		$title = $post -> GetTitle();
		$content = $post -> GetContent();
		$xhtml = "
		<h3>Redigera inlägg:</h3>
			<form action='' method='post' class='post'> 
				<p>Titel:</p><input type='text' name='$this->title' value='$title' style='width:350px; height:30px; '/></br>
				<p>Inlägg:</p><textarea class='ckeditor' type='text' name='$this->content' style='width:550px; height:320px;' >$content</textarea></br>
				<input type='submit' name='$this->editsubmit' value='Spara' class='delete' style='margin-top:10px;'/>
				</form>";
		return $xhtml;
	}
	//Edit och delete knappar
	public function MakeChangesButtons() {
		$xhtml = "
		<form action='' method='post'> 
			<input type='submit' name='$this->delete' value='Radera' class='delete'/>
			<input type='submit' name='$this->edit' value='Redigera' class='delete'/>
		</form>";
		return $xhtml;
	}

	public function PostID() {
		if (isset($_POST[$this -> postID]) == true) {
			return $_POST[$this -> postID];
		}
	}

	public function Title() {
		if (isset($_POST[$this -> title]) == true) {
			return $_POST[$this -> title];
		}
		return null;

	}

	public function Content() {
		if (isset($_POST[$this -> content]) == true) {
			return $_POST[$this -> content];
		}
		return null;
	}

	public function TriedToSubmit() {
		if (isset($_POST[$this -> submit]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToEditSubmit() {
		if (isset($_POST[$this -> editsubmit]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToDelete() {
		if (isset($_POST[$this -> delete]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToEdit() {
		if (isset($_POST[$this -> edit]) == true) {
			return true;
		}
		return false;
	}

	public function NewPostMess($status) {
		switch ($status) {
			case self::POST_EMPTY :
				return "<p class='warning'>Titel eller inlägget är tomt!</p>";
				break;
			case self::POST_SUCCESS :
				return "<p class='Success'>Ditt inlägg sparades!</p>";
				break;
			case self::POST_FAILED :
				return "<p class='warning'>Ditt inlägg gick inte att spara!</p>";
				break;
			case self::POST_SAVED :
				return "<p class='Success'>Ditt inlägg sparades!</p>";
				break;
			case self::POST_DELETED :
				return "<p class='warning'>Ditt inlägg raderades!</p>";
				break;
			case self::POST_NOT_DELETED :
				return "<p class='warning'>Ditt inlägg raderades inte!</p>";
				break;
			default
			;
				break;
		}
	}

}
